//
//  GYUser.m
//  GoodLuck
//
//  Created by apple on 2018/3/1.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYUser.h"

static NSString *userPath = @"user.plist";

@implementation GYUser

- (instancetype)initWithDic:(NSDictionary *)dic {
    if ([super init]) {
        [self setValuesForKeysWithDictionary:dic];
    }
    return  self;
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    
}

- (void)saveUserInfo {
    [NSKeyedArchiver archiveRootObject:self toFile:[NSString CachesDir:userPath]];
}

static GYUser *_userInfo;

+ (GYUser *)loadUserInfo {
    
    //    //1.首先检测这个地址文件存不存在
    BOOL isExistence = [[NSFileManager defaultManager]isExecutableFileAtPath:[NSString CachesDir:userPath]];
    
    if (_userInfo != nil && isExistence) {
        return _userInfo;
    }
    
    //解档
    _userInfo = [NSKeyedUnarchiver unarchiveObjectWithFile:[NSString CachesDir:userPath]];
    
    return _userInfo;

}

+ (BOOL)removeUserInfo {
    NSError *error;
    [[NSFileManager defaultManager] removeItemAtPath:[NSString CachesDir:userPath] error:&error];
    if (error == nil) {
        NSLog(@"删除信息没错误...");
        return YES;
    }else {
        NSLog(@"删除个人信息失败....====%@",error);
        return NO;
    }
}


- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:_key forKey:@"key"];
    [aCoder encodeObject:_userid forKey:@"userid"];
    [aCoder encodeObject:_username forKey:@"username"];
    
    
}
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    if (self) {
        _key = [aDecoder decodeObjectForKey:@"key"];
        _userid = [aDecoder decodeObjectForKey:@"userid"];
        _username = [aDecoder decodeObjectForKey:@"username"];
    }
    return self;
}


- (NSString *)description {
    NSArray *keys = @[@"key",@"userid",@"username"];
    return [NSString stringWithFormat:@"%@",[self dictionaryWithValuesForKeys:keys]];
}

@end
